package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.MedicineConstants;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IMedMoService;
import com.ruoyi.system.service.IMedStockRecordService;
import com.ruoyi.system.service.IMedicineService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.MedicalOrderMapper;
import com.ruoyi.system.service.IMedicalOrderService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 处方管理Service业务层处理
 *
 * @author yangxp
 * @date 2020-08-06
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class MedicalOrderServiceImpl extends ServiceImpl<MedicalOrderMapper, MedicalOrder> implements IMedicalOrderService {

	private final IMedMoService medMoService;

	private final IMedicineService medicineService;

	private final MedicalOrderMapper medicalOrderMapper;

	private final IMedStockRecordService medStockRecordService;

	@Override
	@Transactional(rollbackFor = BaseException.class)
	public boolean saveMo(MedicalOrder medicalOrder) {
		List<MedMo> medicineList = medicalOrder.getMedicineList();
		if (CollectionUtil.isEmpty(medicineList)) {
			return false;
		}
		String moId = IdUtil.fastSimpleUUID();
		DateTime dateTime = DateUtil.date();
		//总价累积
		BigDecimal onePrice = new BigDecimal(0);

		//获取medId,对药品库存做相应的减少
		for (MedMo item : medicineList) {
			String medId = item.getMedId();
			Medicine medicine = medicineService.getOne(new LambdaQueryWrapper<Medicine>().eq(Medicine::getMedId, medId));
			//获取并设置新的库存信息
			BigDecimal stock = medicine.getStock();
			BigDecimal medGram = item.getMedGram();
			BigDecimal newStock = NumberUtil.sub(stock, medGram);
			medicine.setStock(newStock);
			medicine.setGmtModified(dateTime);

			BigDecimal oneMedPrice = NumberUtil.mul(medicine.getMarketPrice(), medGram);
			onePrice = NumberUtil.add(onePrice, oneMedPrice);

			//写进库存改变记录表
			MedStockRecord medStockRecord = new MedStockRecord();
			medStockRecord.setMsrId(IdUtil.fastSimpleUUID());
			medStockRecord.setMoId(moId);
			medStockRecord.setChangeAmount(medGram);
			medStockRecord.setOldStock(stock);
			medStockRecord.setNewStock(newStock);
			medStockRecord.setMedId(item.getMedId());
			medStockRecord.setMedName(medicine.getMedName());
			medStockRecord.setChangeType(MedicineConstants.CHANGE_TYPE_SUB);
			medStockRecord.setGmtCreate(dateTime);
			medStockRecord.setGmtModified(dateTime);
			//保存库存更新记录表
			medStockRecordService.save(medStockRecord);

			//设置关联表相关信息,以便批量保存
			item.setMoId(moId);
			item.setGmtCreate(dateTime);
			item.setGmtModified(dateTime);
			//保存新库存
			medicineService.updateById(medicine);

		}
		//批量保存关联表
		boolean saveBatch = medMoService.saveBatch(medicineList);

		//保存处方记录
		medicalOrder.setGmtCreate(dateTime);
		medicalOrder.setGmtModified(dateTime);
		medicalOrder.setMoId(moId);
		BigDecimal moPrice = NumberUtil.mul(onePrice, medicalOrder.getMoAmount());
		medicalOrder.setMoPrice(moPrice);
		boolean save = this.save(medicalOrder);

		return saveBatch && save;
	}

	@Override
	public List<MedicalOrder> listMedicalOrder(Wrapper wrapper) {
		return medicalOrderMapper.selectMedicalOrderList(wrapper);
	}

	@Override
	public List<Map<String, String>> listDailyMoCountPrice() {

		return medicalOrderMapper.selectDailyMoCountPrice();
	}


}
